﻿
''' <summary>
''' Will serve as the file/folder browser dialog
''' will be based on ubuntu's file/folder browser dialog
''' </summary>
''' <remarks></remarks>
Class frmMTExplorer
    'TODO:
    'make the functions for this explorer

    Private mSelectedPath As String
    Private mSelectedFile As String
    Private mBrowserType As BrowserTypeEnum
    Private mTypeSelected As TypeSelected


    Enum TypeSelected
        File
        Folder
    End Enum

    Enum BrowserTypeEnum
        WithFile
        FolderOnly
    End Enum

    ''' <summary>
    ''' returns the selected path
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property SelectedPath() As String
        Get
            Return mSelectedPath
        End Get
    End Property

    ''' <summary>
    ''' Returns the selected file name
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property SelectedFile() As String
        Get
            Return mSelectedFile
        End Get
    End Property

    Public ReadOnly Property SelectedType() As TypeSelected
        Get
            Return mTypeSelected
        End Get
    End Property


    Public Overloads Function ShowDialog(ByVal BrowserType As BrowserTypeEnum) As DialogResult

        'set the browser type accordingly
        mBrowserType = BrowserType

        'show the form
        Return Me.ShowDialog()

    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        'set the vars
        mSelectedPath = txtSelected.Text
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub RefreshDirectory(ByVal Directory As String)
        lstContents.Items.Clear()

        For Each folderEntry As String In IO.Directory.GetDirectories(Directory)
            Dim tempFolderItem As New ListViewItem

            'make a temp item then add it to the listview
            With tempFolderItem
                .Name = "fldr_" & folderEntry 'add fldr_ prefix to let the listview would know
                .Text = folderEntry.Replace(IO.Directory.GetCurrentDirectory, "")
                .ImageKey = "folder"
            End With
            'lstContents.Items.Add(folderEntry.Replace(IO.Directory.GetDirectoryRoot(folderEntry), ""), "folder")
            lstContents.Items.Add(tempFolderItem)

        Next

        If mBrowserType = BrowserTypeEnum.WithFile Then
            For Each fileEntry As String In IO.Directory.GetFiles(Directory)
                Dim tempFileItem As New ListViewItem

                'make a temp item then add it to the listview
                With tempFileItem
                    .Name = "file_" & fileEntry
                    .Text = fileEntry
                    .ImageKey = "file"
                End With
                ' lstContents.Items.Add(fileEntry.Replace(IO.Directory.GetDirectoryRoot(fileEntry), ""), "file")
                lstContents.Items.Add(tempFileItem)

            Next
        End If


    End Sub

    Private Sub RefreshLists()
        'reset
        lstPlaces.Items.Clear()

        'set the trees/lists
        For Each DriveName As System.IO.DriveInfo In My.Computer.FileSystem.Drives
            Select Case DriveName.DriveType
                Case IO.DriveType.CDRom

                Case IO.DriveType.Fixed

                Case IO.DriveType.Network

                Case IO.DriveType.NoRootDirectory

                Case IO.DriveType.Removable

                Case IO.DriveType.Ram

                Case IO.DriveType.Unknown

                Case Else

            End Select
            Try
                lstPlaces.Items.Add(DriveName.Name, DriveName.Name.Replace("\", "") & " (" & DriveName.VolumeLabel & ")", "")
            Catch ex As Exception

            End Try

        Next
    End Sub

    Private Sub frmMTExplorer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshLists()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        RefreshLists()
    End Sub

    Private Sub lstDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlaces.SelectedIndexChanged
        If lstPlaces.SelectedItems.Count > 0 Then
            'get the folders from the selected drive
            lstContents.Items.Clear()
            ' MsgBox(lstPlaces.SelectedItems(0).Name)
            RefreshDirectory(lstPlaces.SelectedItems(0).Name)
            mSelectedPath = lstPlaces.SelectedItems(0).Name
            mSelectedFile = ""
            txtSelected.Text = mSelectedPath & mSelectedFile
        End If
    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click

    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click

    End Sub

    Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click

    End Sub

    Private Sub lstContents_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstContents.ItemActivate
        If lstContents.SelectedItems.Count > 0 Then
            'MsgBox(lstContents.SelectedItems(0).Name)
            Dim mSelectedDir As String = lstContents.SelectedItems(0).Name

            'check the item activated
            If mSelectedDir.StartsWith("fldr_") Then
                mSelectedPath = mSelectedDir.Remove(0, 5)
                RefreshDirectory(mSelectedPath)
                txtSelected.Text = mSelectedPath
                mTypeSelected = TypeSelected.Folder

            ElseIf mSelectedDir.StartsWith("file_") Then
                'select this file for now
                mSelectedFile = mSelectedDir.Remove(0, 5)
                txtSelected.Text = mSelectedFile

                'RefreshDirectory(mSelectedDir.Remove(0, 5))
                mTypeSelected = TypeSelected.File
                Me.DialogResult = Windows.Forms.DialogResult.OK

            End If

            'txtSelected.Text = mSelectedDir
        End If
    End Sub

    Private Sub lstContents_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContents.SelectedIndexChanged
        If lstContents.SelectedItems.Count > 0 Then
            If lstContents.SelectedItems(0).Name.StartsWith("fldr_") Then
                mSelectedPath = lstContents.SelectedItems(0).Name.Remove(0, 5)
                txtSelected.Text = mSelectedPath
                mTypeSelected = TypeSelected.Folder
            ElseIf lstContents.SelectedItems(0).Name.StartsWith("file_") Then
                mTypeSelected = TypeSelected.File
                mSelectedFile = lstContents.SelectedItems(0).Name.Remove(0, 5)
                txtSelected.Text = mSelectedFile
            End If
        End If
    End Sub

End Class